package com.fy56.platform.logistics.common;

import com.fy56.platform.logistics.service.common.ILogisticsBasisService;
import com.fy56.platform.logistics.service.wx.ILogisticsSlaveBasisService;
import com.fy56.platform.logistics.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * 基础从库Controller
 * Created by Ning on 2018/5/16.
 */
@RestController
@RequestMapping("/controller/slave/basis")
public class SlaveBasisController extends BaseController {
    @Autowired
    private ILogisticsBasisService iLogisticsBasisService;

    @Autowired
    private ILogisticsSlaveBasisService iLogisticsSlaveBasisService;

    @RequestMapping(value = "/findList", method = RequestMethod.POST)
    @ResponseBody
    public Page<Map<String, Object>> findList(Integer page, Integer rows, BaseEntity basisEntity, HttpServletRequest request, String db) {
        Page<Map<String, Object>> pageBean =null;
        //排除掉国标信息表 国标信息，城市，省份暂时可以所有企业公用sys_logink_codevalue as o
        if(StringUtil.isNotEmpty(basisEntity.getTable_()) && ("sys_logink_codevalue as o".equals(basisEntity.getTable_()) || "tb_city as o".equals(basisEntity.getTable_()) || "tb_provincial as o".equals(basisEntity.getTable_()))){
            basisEntity.setWhere_(getQueryParam(request));
        }else {
            String whereSql = getQueryParam(request);
            basisEntity.setWhere_(whereSql);
        }
        String type = request.getParameter("_type");
        if(basisEntity.getTable_().indexOf(" where ")!=-1) {
            basisEntity.setWhere_(basisEntity.getWhere_().replaceFirst(" where "," and "));
        }
        if("slave".equals(db)){
            pageBean = iLogisticsSlaveBasisService.findPage(basisEntity,page, rows);
        }else{
            pageBean = iLogisticsBasisService.findPage(basisEntity,page, rows);
        }
        return pageBean;
    }

    @RequestMapping(value = "/findColumnConfig", method = RequestMethod.POST)
    @ResponseBody
    public String findColumnConfig(TbBasisColumn basisColumn, HttpServletRequest request) {
        return "";
    }

    public static String getQueryParam(HttpServletRequest request) {
        //审核列表页面,如果不需要workflow的话,就传一个auditUseFlag = false 的参数,否则就不传.
        String auditUseFlag = request.getParameter("auditUseFlag");
        if(StringUtil.isEmpty(auditUseFlag)){
            auditUseFlag = "true";
        }
        StringBuffer sb = new StringBuffer();
        String currentRoleId="";//被审核IDS
        String dateType="";
        for (Object key : request.getParameterMap().keySet()) {
            String k = key.toString();
            if (k.length() > 3) {
                String prev = k.substring(0, 3);
                if(prev.equals("_c_")){
                    String value = ((Object[]) request.getParameterMap().get(key))[0].toString();
                    if (!StringUtil.isEmpty(value)) {
                        if("_c_o.auditState".equals(k)){
                            if(!auditUseFlag.equals("false")){
                                if("∵1".equals(value)){
                                    currentRoleId=" and 1=0";
                                }
                            }
                        }
                        if(k.substring(0, 4).equals(prev + AbstractModel.SPLIT)){
                            sb.append(sb.length() == 0 ? " where " : " and ").append(value);
                        }else{
                            String[] keys = k.split(",");
                            if (keys.length == 1) {
                                StringUtil.getWhere(sb, keys[0].substring(3), value, true);
                            } else {
                                sb.append(sb.length() == 0 ? " where (" : " and (");
                                boolean first = true;
                                for (String _key : keys) {
                                    if (!first) {
                                        sb.append(" or ");
                                    }
                                    StringUtil.getWhere(sb, _key.substring(3), value, false);
                                    first = false;
                                }
                                sb.append(")");
                            }
                        }
                    }
                }else if(prev.equals("_s_")){
                    //开始时间
                    String value = ((Object[]) request.getParameterMap().get(key))[0].toString();
                    if(StringUtil.isEmpty(dateType)){
                        dateType=k.substring(3);
                    }
                    if (!StringUtil.isEmpty(value)){
                        sb.append(sb.length() == 0 ? " where " : " and ").append(dateType).append(">='").append(value).append(value.indexOf(" ") !=- 1 ? ":00" : " 00:00:00").append("'");
                    }
                }else if(prev.equals("_e_")){
                    //结束时间
                    String value = ((Object[]) request.getParameterMap().get(key))[0].toString();
                    if(StringUtil.isEmpty(dateType)){
                        dateType=k.substring(3);
                    }
                    if (!StringUtil.isEmpty(value)){
                        sb.append(sb.length() == 0 ? " where " : " and ").append(dateType).append("<='").append(value).append(value.indexOf(" ") !=- 1 ? ":59" : " 23:59:59").append("'");
                    }
                }else if(prev.equals("_d_")){
                    //时间类型
                    dateType = ((Object[]) request.getParameterMap().get(key))[0].toString();
                    if(StringUtil.isNotEmpty(dateType)){
                        dateType=dateType.substring(1);
                    }
                }
            }
        }
        return sb.toString()+currentRoleId;
    }
}
